# 引用
import pygame
import os
import sys
import random
from pygame.locals import *
pygame.init()
UNIT = 16
WIDTH = 800
HEIGNT = 480
W_I_D = 50
H_E_I = 30
fpsClock = pygame.time.Clock()
win = pygame.display.set_mode((WIDTH, HEIGNT))
# 定义常量
# 定义所有的类


class RBTT:
    snakex = 0
    snakey = 0
    berryx = 0
    berryy = 0


class BG:
    # 定义属性颜色，长，宽，窗口,图片
    win = None
    width = int(WIDTH/UNIT)
    height = int(HEIGNT/UNIT)
    background = pygame.Color(0, 0, 0)  # cornflower blue
    image = pygame.image.load('wall.png')

    # 构造函数————init————保存窗口
    def __init__(self, win):
        self.win = win

    # 绘制函数draw()，先全部涂成颜色，再画上围墙
    def draw(self):
        win.fill(self.background)
        # 绘制上下围墙
        for i in range(self.width):
            self.win.blit(self.image, (i*UNIT, 0))
            self.win.blit(self.image, (i*UNIT, HEIGNT-UNIT))
            # 绘制左右围墙
        for i in range(self.height):
            self.win.blit(self.image, (0, i*UNIT))
            self.win.blit(self.image, (WIDTH-UNIT, i*UNIT))


class Berry:
    # 基本属性，x,y,图片,窗口
    win = None
    x = 10
    y = 10
    berry = pygame.image.load('berry.png')

    def __init__(self, win):
        self.win = win
        self.berry.set_colorkey((255, 0, 255))

    def draw(self):
        self.win.blit(self.berry, (self.x*UNIT, self.y*UNIT))

    def randompos(self):
        self.x = random.randint(1, W_I_D-2)
        self.y = random.randint(1, H_E_I-2)

    def eat(self, x, y):
        if (self.x == x and self.y == y):
            self.x = random.randint(1, W_I_D-2)
            self.y = random.randint(1, H_E_I-2)


class Snak:
    win = None
    x = 24
    y = 15
    zz = 0
    snak = [(x, y), (x-1, y), (x-2, y)]
    snake = [(x, y), (x-1, y), (x-2, y)]
    image = pygame.image.load('snake.png')
    fx = 1

    def __init__(self, win):
        self.win = win

    def eat(self, x, y):
        if (self.x == x and self.y == y):
            if(self.fx == 1):
                self.snak.insert(len(self.snak)-1,
                                 (self.x-len(self.snak)-1, self.y))
            elif(self.fx == 2):
                self.snak.insert(len(self.snak)-1,
                                 (self.x-len(self.snak)+1, self.y))
            elif(self.fx == 3):
                self.snak.insert(len(self.snak)-1,
                                 (self.x-len(self.snak), self.y-1))
            else:
                self.snak.insert(len(self.snak)-1,
                                 (self.x-len(self.snak), self.y+1))

    def die(self):
        if(self.x <= 0 or self.x >= 49 or self.y <= 0 or self.y >= 29):
            self.snak = self.snake
            x = self.snake[0][0]
            y = self.snake[0][1]

    def draw(self):
        stc = 0
        # 判断是第几张图片
        if(self.fx == 1):
            stc = 3+self.zz
        elif(self.fx == 2):
            stc = 1+self.zz
        elif(self.fx == 3):
            stc = 5+self.zz
        else:
            stc = 7+self.zz
        # 画蛇头
        self.win.blit(self.image, (self.snak[0][0]*UNIT, self.snak[0]
                                   [1]*UNIT, UNIT, UNIT), (stc*UNIT, 0, UNIT, UNIT))
        # 画蛇身
        for i in range(1, len(self.snak)-1):
            # 判断蛇身画在哪
            self.win.blit(
                self.image, (self.snak[i][0]*UNIT, self.snak[i][1]*UNIT, UNIT, UNIT), (8*UNIT, 0, UNIT, UNIT))
        self.zz = 1-self.zz

    def updaet(self):
        keys = pygame.key.get_pressed()
        if keys[pygame.K_LEFT] and self.fx != 2:
            self.x -= 1
            self.fx = 1
        elif keys[pygame.K_RIGHT] and self.fx != 1:
            self.x += 1
            self.fx = 2
        elif keys[pygame.K_UP] and self.fx != 4:
            self.y -= 1
            self.fx = 3
        elif keys[pygame.K_DOWN] and self.fx != 3:
            self.y += 1
            self.fx = 4
        else:
            if self.fx == 1:
                self.x -= 1
            elif self.fx == 2:
                self.x += 1
            elif self.fx == 3:
                self.y -= 1
            elif self.fx == 4:
                self.y += 1
        self.snak.insert(0, (self.x, self.y))
        del self.snak[len(self.snak)-1]
# class Info:


    # 定义蛇，草莓，背景和记分牌
bg = BG(win)
berry = Berry(win)
#berry = Berry()
snak = Snak(win)
#info = Info()
berry.randompos()
font = pygame.font.SysFont('华文琥珀', 32)
text = font.render('贪吃蛇', 0, (255, 0, 0))
while True:
    # 画背景

    bg.draw()
    win.blit(text, (200, 50))
    # 更新蛇，草莓和记分牌
    # 画蛇，草莓和记分牌
    snak.updaet()
    snak.die()
    snak.eat(berry.x, berry.y)
    berry.eat(snak.x, snak.y)
    # info.update()
    berry.draw()
    snak.draw()
    # info.draw()
    # 是否退出
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
    pygame.display.update()
    fpsClock.tick(10)
